//
//  AttentionPublicViewController.m
//  AAIM
//
//  Created by 远的小布丁 on 2020/1/20.
//  Copyright © 2020 AAIM. All rights reserved.
//

#import "AttentionPublicViewController.h"
#import "PublicTableViewCell.h"
#import "PublicArticleViewController.h"
#import "PublicQRCodeViewController.h"
@interface AttentionPublicViewController ()<UITableViewDelegate,UITableViewDataSource,PublicTableViewCellDelegate>
@property (nonatomic,strong)UITableView * publicTableView;
@property (nonatomic,strong)NSMutableArray * dataAry;
@property (nonatomic,copy)NSString * inviterFuuid;//邀请者的fuuid，扫二维码获得
@property (nonatomic,strong)PublicModel * pubModel;
@end

@implementation AttentionPublicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.publicTableView];
   
    _dataAry = [NSMutableArray array];
    [self getPublicInfo];
}

- (void)getPublicInfo{
    KWeakSelf;
    [RequestData PostPublicInfoWithToken:[LoginManager shareLoginManager].userModel.token userUUid:[LoginManager shareLoginManager].userModel.uuid withPublicUDID:_udid Success:^(NSDictionary * _Nonnull Dict) {
       weakSelf.pubModel = [PublicModel mj_objectWithKeyValues:Dict[@"data"]];
        [weakSelf.dataAry addObject:weakSelf.pubModel];
        NSLog(@"公众号资料页：%@",Dict);
        [weakSelf.publicTableView reloadData];
    } failure:^(NSString * _Nonnull errMsg) {
        [self showTipHudWithText:errMsg];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataAry.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    _pubModel = _dataAry[indexPath.row];
    CGFloat height = [self calculationCellHeight:_pubModel.describe] +80;
    return height;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PublicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AttentionCell"];
    if (!cell) {
        cell = [[PublicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AttentionCell" withCellType:@"AttentionCell"];
    }
    _pubModel = _dataAry[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    cell.cellType = @"AttentionCell";
    cell.indexPath = indexPath;
    cell.model = _pubModel;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
   return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

//cell代理
-(void)intoAttentionPageDelegate:(UIButton *)sender{
    NSInteger tag = sender.tag;
    if (tag == 1001) {
        //进入公众号
        PublicArticleViewController * articleVC = [[PublicArticleViewController alloc]init];
        articleVC.publicUdid = _pubModel.udid;
        articleVC.gk_navTitle = _pubModel.title;
        articleVC.publicModel = _pubModel;
        [self.navigationController pushViewController:articleVC animated:YES];
    }else{
//      关注或者取消关注
        sender.selected = !sender.selected;
        [self attentionPublicNumber:[NSString stringWithFormat:@"%d",sender.selected] with:sender];
    }
}

- (void)touchesToPublicInfoPage{
   PublicQRCodeViewController * qrCodeVC = [[PublicQRCodeViewController alloc]init];
   qrCodeVC.gk_navTitle = @"二维码";
   qrCodeVC.udid = _pubModel.udid;
   [self.navigationController pushViewController:qrCodeVC animated:YES];
}

//关注
- (void)attentionPublicNumber:(NSString *)isAdd with:(UIButton *)sender{
    KWeakSelf;
    [RequestData PostAttentionPublicWithToken:[LoginManager shareLoginManager].userModel.token publicUdid:_pubModel.udid withUserUuid:[LoginManager shareLoginManager].userModel.uuid withIsAdd:isAdd withFuuid:_inviterFuuid Success:^(NSDictionary * _Nonnull Dict) {
        NSLog(@"%@",Dict);
        if ([Dict[@"code"] integerValue] == 200) {
           
            if ([isAdd integerValue]) {
                 [weakSelf showTipHudWithText:@"关注成功"];
                [[UserDataBaseTool shareUserDataBaseTool] savePublicModel:weakSelf.pubModel type:3];
                sender.selected = YES;
                
            }else{
            [weakSelf showTipHudWithText:@"取消关注成功"];
                [[UserDataBaseTool shareUserDataBaseTool]deletePublicUdid:weakSelf.pubModel.udid];
                sender.selected = NO;
            }
        }
        
    } failure:^(NSString * _Nonnull errMsg) {
        [weakSelf showTipHudWithText:errMsg];
    }];
}

- (UITableView *)publicTableView{
    if (!_publicTableView) {
        _publicTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.gk_navigationBar.bottom, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
        _publicTableView.delegate = self;
        _publicTableView.dataSource = self;
        _publicTableView.backgroundColor = [UIColor whiteColor];
        _publicTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _publicTableView.tableHeaderView = [UIView new];
        UIView * footView = [UIView new];
        footView.backgroundColor = KHEXCOLOR(@"#F4F4F4");
        footView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-[self calculationCellHeight:@"每天分享国内外湖两旺与投资消息速递，第一"] - 94);
        _publicTableView.tableFooterView = footView;
        _publicTableView.rowHeight = UITableViewAutomaticDimension;
    }
    return _publicTableView;
}

- (CGFloat)calculationCellHeight:(NSString *)content{
    CGRect rect = [content boundingRectWithSize:CGSizeMake(kScreenWidth - 88, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: kMainFont(12)} context:nil];
    return rect.size.height + 80.f;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
